package com.yulang.userapi.controller;

import com.yulang.userapi.pojo.User;
import com.yulang.userapi.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public User save(@RequestBody User user){
        user.setId(UUID.randomUUID().toString());
        userService.save(user);
        return user;
    }

    @PutMapping("/{id}")
    public void update(@PathVariable String id){
        return;
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable String id){
        return;
    }

    @GetMapping("/{id}")
    public User get(@PathVariable String id, HttpServletRequest request){
        User user1 = (User)request.getAttribute("user");
        if(user1!=null){
            User user = userService.getById(1);
            if(user.getName().equals(user1.getName())){
                return user;
            }
        }
        return null;
    }

}
